
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Nginx 的负载均衡器如何处理会话保持 - 学习卡片</title>
      <style>
        body { font-family: sans-serif; background-color: #f0f8ff; color: #333; display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
        .header h1 { font-size: 32px; }
        .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; width: 100%; max-width: 1200px; }
        .card-container { perspective: 1200px; cursor: pointer; height: 250px; }
        .card { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transition: transform 0.7s; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
        .card-container.flipped .card { transform: rotateY(180deg); }
        .card-face { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; flex-direction: column; box-sizing: border-box; border-radius: 16px; background-color: #fff; padding: 24px; }
        .card-back { background-color: #f0fff4; transform: rotateY(180deg); justify-content: space-between; }
        .card-category { font-size: 14px; color: #0052d9; margin-bottom: 8px; font-weight: 500; }
        .card-question { font-size: 20px; font-weight: 500; flex-grow: 1; display: flex; align-items: center; justify-content: center; text-align: center; }
        .card-answer-wrapper { flex-grow: 1; overflow-y: auto; }
        .card-answer { font-size: 15px; line-height: 1.7; }
        .card-footer { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 16px; margin-top: 16px; }
        .card-source { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 12px; margin-top: 12px; }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>Nginx 的负载均衡器如何处理会话保持 - 学习卡片</h1>
      </div>
      <div class="grid-container">
        
    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">理论</div>
          <div class="card-question">在Nginx负载均衡中，“会话保持”（Session Persistence）的定义和主要目的是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">理论</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">会话保持是一种确保用户的后续请求被路由到相同的后端服务器的技术。其主要目的是为了保持用户的会话状态，从而提升用户体验。</div>
          </div>
          <div class="card-source">来源: 文档开篇部分</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">Nginx的`ip_hash`指令是如何实现会话保持的？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`ip_hash`指令通过将客户端的IP地址进行哈希计算，来确保来自同一个IP地址的所有请求都被稳定地路由到同一个后端服务器。</div>
          </div>
          <div class="card-source">来源: 基于 IP 地址的会话保持</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">相较于`ip_hash`，Nginx的`sticky`模块在实现会话保持时有何不同？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`sticky`模块是基于Cookie来实现会话保持的。它会为每个客户端分配一个唯一的Cookie，后续请求将根据这个Cookie被路由到最初设置该Cookie的服务器，而不是基于客户端的IP地址。</div>
          </div>
          <div class="card-source">来源: 基于 Cookie 的会话保持</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">Nginx的`hash`指令提供了哪种灵活的会话保持方式？它可以基于哪些信息进行哈希？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`hash`指令允许使用请求的某些特定部分（例如某个Cookie值或请求头）来作为哈希的依据，从而将具有相同哈希值的请求路由到同一台服务器。这比`ip_hash`更加灵活。</div>
          </div>
          <div class="card-source">来源: 基于 Hash 的会话保持</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">在Nginx的`upstream`配置中，如何启用基于IP地址的会话保持？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">在`upstream`配置块中，直接添加`ip_hash;`指令即可启用基于IP地址的会话保持。</div>
          </div>
          <div class="card-source">来源: 基于 IP 地址的会话保持</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">如何配置Nginx，使其基于请求中名为`session_id`的Cookie值来进行哈希路由？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">在`upstream`配置块中使用`hash $cookie_session_id consistent;`指令，即可配置Nginx根据请求中名为`session_id`的Cookie值进行哈希，以实现会话保持。</div>
          </div>
          <div class="card-source">来源: 基于 Hash 的会话保持</div>
        </div>
      </div>
    </div>

      </div>
    </body>
    </html>
